from pynput.mouse import Controller as mCtrl,Button
from pynput.keyboard import Controller as kCtrl
from pynput.keyboard import Key
from time import time,mktime,strptime,sleep
from ctypes import *


mc = mCtrl()
kc = kCtrl()
gdi32 = windll.gdi32
user32 = windll.user32
hdc = user32.GetDC(None)


# 鼠标点击
def mclick(x,y):
    x = int(x/2)
    y = int(y/2)
    mc.position = (x,y)
    mc.press(Button.left)
    mc.release(Button.left)

# 掐时间
def wait_time(tt = '2023-01-03 21:40:00'):
    tt = strptime(tt,'%Y-%m-%d %H:%M:%S')
    tt = mktime(tt)
    while(True):
        now = time()
        if now >= tt:
            break
    

# 获取屏幕某个点的颜色
def getrgb(x,y):
    pixel = gdi32.GetPixel(hdc, x, y)
    r = pixel & 0x0000ff
    g = (pixel & 0x00ff00) >> 8
    b = pixel >> 16
    return r,g,b

# 查询车票
def query_ticket_console():
    """在chrome 控制台中找到otn -> resources -> merged -> queryLeftTicket_end_js.js?scriptVersion=1.9369 中
        在3600行左右找到 function ay(cV)函数，函数体内打上断点
        点击查询，将ay函数和cV参数保存出来
        通过控制台调用函数查询车票，可跳过间隔限制
        myay = ay
        mvcV = cV
        myay(mycV)
    """
    while(True):
        kc.press(Key.enter)
        kc.release(Key.enter)
        
    
# 查询车票
def query_ticket_click():
    """在chrome 控制台中找到otn -> resources -> merged -> queryLeftTicket_end_js.js?scriptVersion=1.9369 中
        在3600行左右找到 function ay(cV)函数，函数体内打上断点
        点击查询，将ay函数和cV参数保存出来
        通过控制台调用函数查询车票，可跳过间隔限制
        myay = ay
        mvcV = cV
        myay(mycV)
    """
    while(True):
        # 点击查询
        print('点击查询')
        mclick(2913,463)
        # 判断页面加载
        while(True):
            sleep(0.01)
            r,g,b = getrgb(922,823)
            if b > 50:
                continue
            break
        # 预定按钮是否能点击
        r,g,b = getrgb(2944,1490)
        if b > 200 and r < 20:
           break 

# 进入车票详情页           
def check_ticket():
    # 点击预定按钮
    print('进入详情页面')
    mclick(2969,1491)
    # 页面加载
    while(True):
        sleep(0.01)
        r,g,b = getrgb(962,780)
        if b > 200 and r < 100:
            break
    # 勾选乘车人
    print('勾选乘车人')
    mclick(1008,836)
    sleep(0.01)
        
# 提交订单
def submit_order():
    # 点击提交订单
    print('点击提交订单')
    mclick(2014,1300)
    # 等待弹框
    while(True):
        r,g,b = getrgb(2012,1218)
        if r > 200 and b < 20:
            break
    
    # 选择F座
    # mclick(2206,1065)
    # while(True):
    #     r,g,b = getrgb(2205,1050)
    #     if b > 200:
    #         break
    
    # 提交订单，不选座
    print('点击确认订单')
    while(True):
        r,g,b = getrgb(1930,1210)
        if r > 200 and b < 20:
            for i in range(100):
                mclick(2010,1220)
                sleep(0.01)



sleep(2)
wait_time('2023-01-03 23:18:40')
query_ticket_click()
check_ticket()
submit_order()